#include "BasicWindow.h"
#include <QFile>
//#include <QBrush>
#include <QPainter>
#include <QStyleOption>
#include <QApplication>
#include <QDesktopWidget>
#include "CommonUtils.h"
#include "NotifyManager.h"
#include <QDebug>

BasicWindow::BasicWindow(QWidget *parent)
	: QDialog(parent)
{
	setWindowFlags(Qt::FramelessWindowHint);
	//WA_TranslucentBackground
	//WA_TintedBackground
	setAttribute(Qt::WA_TranslucentBackground, true);

	m_colorBackGround = CommonUtils::getDefaultSkinColor();
	QObject::connect(NotifyManager::getInstance(), SIGNAL(signalSkinChanged(const QColor&)),
					 this, SLOT(onSignalSkinChanged(const QColor &)));
	//initTitleBar();
}

void BasicWindow::onSignalSkinChanged(const QColor &color)
{
	m_colorBackGround = color;
	loadStyleSheet(m_styleName);	//������ʽ�����color�ϲ���ȥ
}

void BasicWindow::setTitleBarTitle(const QString &title, const QString &icon)
{
	_titleBar->setTitleContent(title);
	_titleBar->setTitleIcon(icon);

}

void BasicWindow::initTitleBar(ButtonType buttontype)
{
	_titleBar = new TitleBar(this);
	_titleBar->setButtonType(buttontype);
	_titleBar->move(0, 0);

	connect(_titleBar, SIGNAL(signalButtonRestoreClicked()), this, SLOT(onButtonRestoreClicked()));
	connect(_titleBar, SIGNAL(signalButtonMinClicked()), this, SLOT(onButtonMinClicked()));
	connect(_titleBar, SIGNAL(signalButtonCloseClicked()), this, SLOT(onButtonCloseClicked()));
	connect(_titleBar, SIGNAL(signalButtonMaxClicked()), this, SLOT(onButtonMaxClicked()));

}

void BasicWindow::loadStyleSheet(const QString &sheetName)
{
	m_styleName = sheetName;
	QFile file(":/Resources/QSS/" + sheetName + ".css");
	file.open(QIODevice::ReadOnly);

	if (file.isOpen()) {
		setStyleSheet(QString());
		QString qStyleSheet = file.readAll();

		QString r = QString::asprintf("%d", m_colorBackGround.red());
		QString g = QString::asprintf("%d", m_colorBackGround.green());
		QString b = QString::number(m_colorBackGround.blue());

		qStyleSheet += QString("QWidget[titleskin=true]\
								{background-color:rgb(%1,%2,%3);\
		 border-top-right-radius:4px;border-top-left-radius:4px;}\
								QWidget[bottomskin=true]\
								{border-top:1px solid rgba(%1,%2,%3,100);\
								background-color:rgba(%1,%2,%3,50);\
								border-bottom-left-radius:4px;\
								border-bottom-right-radius:4px;}")
								.arg(r).arg(g).arg(b);

		setStyleSheet(qStyleSheet);
	}

	file.close();
}

void BasicWindow::initBackGroundColor()
{
	QStyleOption opt;
	opt.init(this);

	QPainter p(this);
	this->style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

//���໯����ʱ����Ҫ��д��ͼ�¼����ñ���ͼ
void BasicWindow::paintEvent(QPaintEvent *event)
{
	initBackGroundColor();

	return QDialog::paintEvent(event);
}

//ͼ�� ת Բͷ��
QPixmap BasicWindow::getRoundImage(const QPixmap &src, QPixmap &mask, QSize maskSize)
{
	if (maskSize.isNull()) {
		maskSize = mask.size();
	}
	else {
		mask = mask.scaled(maskSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
	}

	
	QImage resultImage(maskSize, QImage::Format_ARGB32_Premultiplied);
	QPainter painter(&resultImage);
	painter.setCompositionMode(QPainter::CompositionMode_Source);
	painter.fillRect(resultImage.rect(), Qt::transparent);
	painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
	painter.drawPixmap(0, 0, mask);
	painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
	painter.drawPixmap(0, 0, src.scaled(maskSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
	painter.end();

	return QPixmap::fromImage(resultImage);

}

void BasicWindow::onShowClose(bool)
{
	//onShowQuit(true);
	close();
}

void BasicWindow::onShowMin(bool)
{
	this->showMinimized();
}

void BasicWindow::onShowHide(bool)
{
	hide();
}

void BasicWindow::onShowNormal(bool)
{
	show();
	activateWindow();
}

void BasicWindow::onShowQuit(bool)
{
	QApplication::quit();
}

void BasicWindow::mousePressEvent(QMouseEvent *event)
{
	if (event->button() & Qt::LeftButton) {
		m_mousePressed = true;
		m_mousePoint = event->globalPos() - pos();
		event->accept();
		////() << "BasicWindow::mousePressEvent";
	}

	
	//return QDialog::mousePressEvent(event);
}

//                mouseReleaseEvent
void BasicWindow::mouseReleaseEvent(QMouseEvent *event)
{
	m_mousePressed = false;
	////() << "BasicWindow::mouseReleaseEvent";
	//return QDialog::mouseReleaseEvent(event);
}

void BasicWindow::mouseMoveEvent(QMouseEvent *event)
{	
	////() << m_mousePressed;
	////() << (event->button() == Qt::MouseButton::LeftButton);
	if (m_mousePressed /*&& (event->button() & Qt::MouseButton::LeftButton)*/) {
		move(event->globalPos() - m_mousePoint);
		//move(0,0);
		event->accept();
		////() << "BasicWindow::mouseMoveEvent";
	}
	
	return QDialog::mouseMoveEvent(event);
}

void BasicWindow::onButtonMinClicked()
{
	if (Qt::Tool == (windowFlags() & Qt::Tool)) {
		hide();
	}
	else {
		showMinimized();
	}
}

void BasicWindow::onButtonRestoreClicked()
{
	QPoint windowPoint;
	QSize windowSize;
	_titleBar->getRestoreInfo(windowPoint, windowSize);

	setGeometry(QRect(windowPoint, windowSize));
}

void BasicWindow::onButtonCloseClicked()
{
	close();
}

void BasicWindow::onButtonMaxClicked()
{
	_titleBar->saveRestoreInfo(pos(), QSize(width(), height()));

	QRect desktopRect = QApplication::desktop()->availableGeometry();
	QRect factRect (desktopRect.x() - 3, desktopRect.y() - 3,desktopRect.width() + 6, desktopRect.height() + 6);

	setGeometry(factRect);
}

BasicWindow::~BasicWindow()
{

}
